import { _decorator, Component, Node, input, Input, EventTouch, Vec3, Collider2D, IPhysics2DContact, Contact2DType, isValid, RigidBody2D, find } from 'cc';
import { Award } from './Award';
import { GameSceneUI, GameState } from './GameSceneUI';
import { AudioManager } from './AudioManager';
const { ccclass, property } = _decorator;
@ccclass('Player')
export class Player extends Component {
    //记录点击屏幕次数，开始游戏时，点击屏幕两次才能射出球体
    TouchTimes:number = 0;

    //记录第一次点击屏幕和第二次的时间 由于是双击开始，双击间隔不能太长，否则不算双击
    TouchStartTime1:number=0;
    TouchStartTime2:number=0;
    //主要记录点击第一次屏幕后的时间，如果没有在100ms内继续点击，就不算连续点击
    DelayTime:number = 0;

    //球体节点，用于在发射出球体之前控制球的运动和玩家同步
    BallBullet:Node=null;

    //玩家刚体组件
    collider:Collider2D = null;

    cavasRight = 0;
    cavasLeft = 0;

    protected onLoad(){
        const canvas = find('Canvas');
        this.cavasRight = canvas.position.x;
        this.cavasLeft = -canvas.position.x;

        input.on(Input.EventType.TOUCH_MOVE,this.onTouchMove,this);
    }

    protected onDestroy(){
        input.off(Input.EventType.TOUCH_MOVE,this.onTouchMove,this);
        if (this.collider) {
            // 注销碰撞回调
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContac, this);
        }
    }

    start() {
        const player_body = this.node.getChildByName('Body');
        this.collider = player_body.getComponent(Collider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContac, this);
        }
    }

    onBeginContac(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null)
    {
        // 初始设置
        if(otherCollider.node.parent)
        {
            const AwardNode = otherCollider.node.parent.getComponent(Award)
            if(AwardNode && AwardNode.getContac()==false)
            {
                AwardNode.Contac();
                AudioManager.getInstance().playSFX();
                this.scheduleOnce(() => {
                    if(isValid(otherCollider))
                    {
                        otherCollider.node.destroy();
                    }
                    GameSceneUI.getInstance().GetAward(AwardNode.getAwardType());
                });
            }
            
        }
    }

    //监测手指在屏幕上移动
    onTouchMove(event:EventTouch){
        const gamestate = GameSceneUI.getInstance().getGameState();
        if(gamestate == GameState.Pause || gamestate == GameState.Over)
        {
            return;
        }

        const p = this.node.position;

        let targetPosition = new Vec3(p.x+event.getDeltaX(),p.y,p.z);
        

        //控制玩家的操作不要超出边界
        if(targetPosition.x < this.cavasLeft){
            targetPosition.x = this.cavasLeft;
        }
        if(targetPosition.x > this.cavasRight){
            targetPosition.x = this.cavasRight
        }

        this.node.setPosition(targetPosition); //将没有问题的坐标参数传入

        //只有在准备阶段球会随玩家移动
        if(GameSceneUI.getInstance().getGameState() == GameState.Init)
        {
            if(this.BallBullet)
            {
                let ballPostion = this.BallBullet.position;
                let ballTargetPosition = new Vec3(ballPostion.x+event.getDeltaX(),ballPostion.y,ballPostion.z);
                if(ballTargetPosition.x < this.cavasLeft){
                    ballTargetPosition.x = this.cavasLeft;
                }
                if(ballTargetPosition.x > this.cavasRight){
                    ballTargetPosition.x = this.cavasRight
                }
                this.BallBullet.setPosition(ballTargetPosition);
            }
            else
            {
                console.error("onTouchMove BallBullet is null");
            }
            
        }
    }

    //游戏帧更新
    protected update(dt:number):void {
        if(this.TouchTimes == 1)
        {
            this.DelayTime += 1;
            if(this.DelayTime >= 100)
            {
                this.DelayTime = 0;
                this.TouchTimes = 0;
                this.TouchStartTime1 = 0;
                this.TouchStartTime2 = 0;
            }
        }
    }

    //用于外部创建好球体后传入
    public SetBallPrefab(ballBullet:Node)
    {
        if(ballBullet == null)
        {
            console.error(" SetBallPrefab is null");
        }
        else
        {
            this.BallBullet = ballBullet;
        }
    }
}


